package com.resou.reader.mine.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.resou.reader.R;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.commom.Constant;
import com.resou.reader.mine.IView.UserInstance;

import butterknife.BindView;
import butterknife.OnClick;

public class NicknameSettingActivity extends BaseActivity {

    @BindView(R.id.nickname_edit)
    AppCompatEditText mNicknameEdit;
    @BindView(R.id.clear_image)
    ImageView mClearImage;

    @Override
    protected void initView() {
        mNicknameEdit.setText(UserInstance.getLoginData().getNickName());
        mNicknameEdit.setSelection(mNicknameEdit.length());
        mNicknameEdit.postDelayed(new Runnable() {
            @Override
            public void run() {
                mNicknameEdit.setFocusable(true);
                mNicknameEdit.hasFocus();
                mNicknameEdit.setFocusableInTouchMode(true);
                mNicknameEdit.requestFocus();
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }, 10);
    }

    @Override
    protected void retryRefreshData() {

    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nickname_setting;
    }

    @OnClick({R.id.clear_image, R.id.title_right_btn})
    public void onClick(View v) {
        Editable nickname = mNicknameEdit.getText();
        switch (v.getId()) {
            case R.id.clear_image:
                if (!TextUtils.isEmpty(nickname.toString().replaceAll(" ", "")))
                    nickname.clear();
                break;
            case R.id.title_right_btn:
                if (isNetwork()) {
                    if (!TextUtils.isEmpty(nickname.toString().replaceAll(" ", ""))) {
                        updateToServer(nickname.toString().toString().replaceAll(" ", ""));
                    } else {
                        Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
                    }
                }

                break;
        }
    }

    private void updateToServer(String nickname) {
        //!UserInstance.getLoginData().getNickName().equals(nickname) && !PageUtils.isEmpty(nickname)
        String currNickName = UserInstance.getLoginData().getNickName();
        if (!TextUtils.isEmpty(nickname) && !nickname.equals(currNickName)) {
            Intent intent = new Intent();
            intent.putExtra("nickname", nickname);
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    public static void startActivity(Context context) {
        ((Activity) context).startActivityForResult(new Intent(context, NicknameSettingActivity.class), Constant.REQUEST_NICKNAME);
    }

    @Override
    public Context getViewContext() {
        return this;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
